package org.beaconframework.ext.rs.cxf;

import org.apache.cxf.jaxrs.client.Client;
import org.apache.cxf.jaxrs.client.WebClient;
import org.beaconframework.ext.rs.WebResource;
import org.beaconframework.ext.rs.WebResourceFactory;
import org.beaconframework.util.CertificateVerificationControl;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;



public class CXFWebResourceFactoryAdapter implements WebResourceFactory,
		InitializingBean, ApplicationContextAware {

	ApplicationContext applicationContext;
	Client client;

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public WebResource createWebResource() {
		WebClient wc = WebClient.fromClient(getClient());

		WebResource wr = new CXFWebResourceFacade(wc);
		return wr;
	}

	public void afterPropertiesSet() {
		org.beaconframework.util.CertificateVerificationControl cvc = applicationContext.getBean(CertificateVerificationControl.BEAN_NAME,CertificateVerificationControl.class);
		if (cvc.isVerificationDisabled()) {
			SslUtil.disableCertificateChecking(client); 
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;

	}

}
